package com.list88zhu.android.sunflower.jetpack.paging;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.arch.paging.LivePagedListBuilder;
import android.arch.paging.PagedList;
import android.support.annotation.NonNull;

import com.list88zhu.android.sunflower.jetpack.room.AppDatabase;
import com.list88zhu.android.sunflower.jetpack.room.Plant;
import com.list88zhu.android.sunflower.jetpack.room.PlantDao;

/**
 * <pre><code>
 *
 *  </code></pre>
 *
 * @author zhuyingming@qmtv.com
 * @date 2018/8/26
 */
public class PlantViewModel extends AndroidViewModel {

    private static final int PAGE_SIZE = 10;
    private static final boolean ENABLE_PLACEHOLDERS = true;

    private PlantDao plantDao;
    private LiveData<PagedList<Plant>> plantPagedList;

    public PlantViewModel(@NonNull Application application) {
        super(application);
        plantDao = AppDatabase.getDefault(application).getPlantDao();
        plantPagedList = new LivePagedListBuilder(plantDao.queryPlants(),
                                new PagedList.Config.Builder()
                                    .setPageSize(PAGE_SIZE)
                                    .setInitialLoadSizeHint(2 * PAGE_SIZE)
                                    .setEnablePlaceholders(ENABLE_PLACEHOLDERS)
                                    .build()).build();
    }

    public void insert(Plant plant) {
        plantDao.insertPlant(plant);
    }

    public PlantDao getPlantDao() {
        return plantDao;
    }


    public LiveData<PagedList<Plant>> getPlantPagedList() {
        return plantPagedList;
    }

}
